/**************************************************************************//**
 * @item     CosyOS-III Kernel
 * @file     sv_tick.c
 * @brief    滴答服务
 * @details  仅在 SysTick 中调用并执行，包括在滴答钩子、定时中断钩子、
             定时查询钩子、定时查询事件等处调用。
 * @author   迟凯峰
 * @version  V1.2.0
 * @date     2025.03.30
 ******************************************************************************/

#include "os_var.h"
#include "sv_com.h"

/**
 @addtogroup CosyOS_内核服务
 @{
 *//**
 \defgroup   滴答服务
 \brief      仅在 SysTick 中调用并执行，包括在滴答钩子、定时中断钩子、
             定时查询钩子、定时查询事件等处调用。
 @{
 */

/**
 \brief      恢复任务
 \param[in]  htask     任务句柄
 \return     错误码
 */
s_ecode_t st_resume_task(s_tasknode_tsp htask) MCUCFG_C51USING
{
	if(htask == OS_NULL){
		return OS_ECODE_TASKNOTSTARTED;
	}
	if(htask->status & OS_STATUS_SUSPENDED){
		sv_resume_task(htask);
		return OS_ECODE_NOERROR;
	}
	if(htask->status < OS_STATUS_STOPPED){
		return OS_ECODE_TASKNOTSUSPENDED;
	}
	if(htask->status == OS_STATUS_STOPPED){
		return OS_ECODE_TASKSTOPPED;
	}
	if(htask->status == OS_STATUS_DELETED){
		return OS_ECODE_TASKNOTSTARTED;
	}
	else{
		return OS_ECODE_DONOTKNOW;
	}
}

/**
 \brief      挂起任务
 \param[in]  htask     任务句柄
 \return     错误码
 */
s_ecode_t st_suspend_task(s_tasknode_tsp htask) MCUCFG_C51USING
{
	if(htask == OS_NULL){
		return OS_ECODE_TASKNOTSTARTED;
	}
	if(htask->status < OS_STATUS_SUSPENDED){
		sv_suspend_task(htask);
		return OS_ECODE_NOERROR;
	}
	if(htask->status & OS_STATUS_SUSPENDED){
		return OS_ECODE_TASKSUSPENDED;
	}
	if(htask->status == OS_STATUS_STOPPED){
		return OS_ECODE_TASKSTOPPED;
	}
	if(htask->status == OS_STATUS_DELETED){
		return OS_ECODE_TASKNOTSTARTED;
	}
	else{
		return OS_ECODE_DONOTKNOW;
	}
}

/**
 \brief      删除任务
 \param[in]  htask     任务句柄
 \return     错误码
 */
s_ecode_t st_delete_task(s_tasknode_tsp htask) MCUCFG_C51USING
{
	if(htask == OS_NULL){
		return OS_ECODE_TASKNOTSTARTED;
	}
	if(htask->status < OS_STATUS_DELETED){
		sv_delete_task(htask);
		return OS_ECODE_NOERROR;
	}
	if(htask->status == OS_STATUS_DELETED){
		return OS_ECODE_TASKNOTSTARTED;
	}
	else{
		return OS_ECODE_DONOTKNOW;
	}
}

/**
 \brief      清除阻塞（状态）
 \param[in]  htask     任务句柄
 \return     错误码
 */
s_ecode_t st_clear_block(s_tasknode_tsp htask) MCUCFG_C51USING
{
	if(htask == OS_NULL){
		return OS_ECODE_TASKNOTSTARTED;
	}
	if(htask->status == OS_STATUS_BLOCKED){
		sv_clear_block(htask);
		return OS_ECODE_NOERROR;
	}
	return OS_ECODE_TASKNOTBLOCKED;
}

/**
 \brief      设置阻塞（时间）
 \param[in]  htask     任务句柄
 \param[in]  tick      滴答周期（阻塞时间）<br>
             +0：清除阻塞<br>
             ~0：无限阻塞
 \return     错误码
 */
s_ecode_t st_set_block(s_tasknode_tsp htask, s_delay_t tick) MCUCFG_C51USING
{
	if(htask == OS_NULL){
		return OS_ECODE_TASKNOTSTARTED;
	}
	if(htask->status == OS_STATUS_BLOCKED){
		sv_set_block(htask, tick);
		return OS_ECODE_NOERROR;
	}
	return OS_ECODE_TASKNOTBLOCKED;
}

/**
 \brief      设置任务优先级
 \param[in]  htask     任务句柄
 \param[in]  npri      新优先级
 \return     错误码
 */
s_ecode_t st_set_taskpri(s_tasknode_tsp htask, s_u8_t npri) MCUCFG_C51USING
{
	if(htask == OS_NULL){
		return OS_ECODE_TASKNOTSTARTED;
	}
	if(htask->status < OS_STATUS_STOPPED){
		if(htask->pri != npri){
			sv_set_taskpri(htask, npri);
			return OS_ECODE_NOERROR;
		}
		else{
			return OS_ECODE_TASKPRIUNCHANGED;
		}
	}
	if(htask->status == OS_STATUS_STOPPED){
		return OS_ECODE_TASKSTOPPED;
	}
	if(htask->status == OS_STATUS_DELETED){
		return OS_ECODE_TASKNOTSTARTED;
	}
	else{
		return OS_ECODE_DONOTKNOW;
	}
}

#if SYSCFG_SEMAPHORE == __ENABLED__
/**
 \brief      获取计数信号量
 \param[in]  hsem      计数信号量句柄
 \return     结果
 \retval     false     失败
 \retval     true      成功
 */
bool st_take_semaphore(s_semaph_tsp hsem) MCUCFG_C51USING
{
	if(hsem->counter){
		(hsem->counter)--;
		return true;
	}
	return false;
}
#endif

#if SYSCFG_MSGQUEUE == __ENABLED__
/**
 \brief      接收消息
 \param[in]  hque      队列句柄
 \return     消息指针
 \retval     NULL      无消息
 */
void *st_recv_msg(s_queue_tsp hque) MCUCFG_C51USING
{
	void *msg = OS_NULL;
	hque->mutex = false;
	
	if(hque->counter){
		if(hque->type == __DYNAMIC__){
			msg = sv_recv_msg_dynamic((sd_queue_tsp)hque);
		}
		else{
			msg = sv_recv_msg_static((ss_queue_tsp)hque);
		}
	}
	
	hque->mutex = true;
	return msg;
}
#endif

/** @} */
/** @} */
